const mysql = require('mysql2/promise')

async function checkColumns () {
  const connection = await mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'GKL9512357',
    database: 'novel_platform'
  })

  try {
    console.log('检查novels表的列结构...\n')

    // 检查novels表结构
    const [columns] = await connection.execute('DESCRIBE novels')
    console.log('novels表的列:')
    columns.forEach(col => {
      console.log(`- ${col.Field}: ${col.Type}`)
    })

    console.log('\n查询SQL中使用的字段:')
    const sqlFields = [
      'title',
      'author',
      'description',
      'cover_url',
      'category',
      'status',
      'word_count',
      'chapter_count',
      'rating',
      'view_count',
      'like_count',
      'is_vip'
    ]

    const missingFields = []
    sqlFields.forEach(field => {
      const exists = columns.find(col => col.Field === field)
      if (exists) {
        console.log(`✅ ${field}: 存在`)
      } else {
        console.log(`❌ ${field}: 不存在`)
        missingFields.push(field)
      }
    })

    if (missingFields.length > 0) {
      console.log('\n缺少的字段:', missingFields.join(', '))
    }

    // 查看实际数据
    console.log('\n前几行数据:')
    const [rows] = await connection.execute('SELECT * FROM novels LIMIT 1')
    if (rows.length > 0) {
      console.log('第一行数据的字段:')
      Object.keys(rows[0]).forEach(key => {
        console.log(`- ${key}: ${rows[0][key]}`)
      })
    }
  } catch (error) {
    console.error('检查失败:', error.message)
  } finally {
    await connection.end()
  }
}

checkColumns()
